import pytest
from errors import Missing
from fake_data import parkings as fake_data
from service import parkings as service


@pytest.fixture
def parking_json():
    return {
        "address": "Moskovskiy pr., 131",
        "opened": True,
        "count_places": 300,
        "count_available_places": 250,
    }


@pytest.fixture
def patched_data(monkeypatch):
    monkeypatch.setattr(
        service.data, "create_parking_json", fake_data.create_parking_json
    )
    monkeypatch.setattr(service.data, "get_parking_json", fake_data.get_parking_json)
    monkeypatch.setattr(
        service.data, "update_parking_json", fake_data.update_parking_json
    )


def test_create_parking_json(patched_data, parking_json):
    result = service.create_parking(**parking_json)
    assert result == {"id": 200, **parking_json}


def test_is_parking_available_true(patched_data):
    result = service.is_parking_available(200)
    assert result is True


@pytest.mark.parametrize(
    "full_parking",
    [
        {
            "address": "Nevskiy pr., 1",
            "opened": True,
            "count_places": 456,
            "count_available_places": 0,
        },
        {
            "address": "Nevskiy pr., 1",
            "opened": False,
            "count_places": 456,
            "count_available_places": 0,
        },
        {
            "address": "Nevskiy pr., 1",
            "opened": False,
            "count_places": 456,
            "count_available_places": 10,
        },
    ],
)
def test_is_parking_available_false(patched_data, full_parking):
    parking = service.create_parking(**full_parking)
    result = service.is_parking_available(parking_id=parking["id"])
    assert result is False


def test_increase_places_count(patched_data, parking_json):
    parking_id = 200
    parking = service.increase_places_count(parking_id)
    assert (
        parking["count_available_places"] == parking_json["count_available_places"] + 1
    )


def test_increase_places_count_wrong_id(patched_data):
    with pytest.raises(Missing):
        service.increase_places_count(parking_id=789)


def test_decrease_places_count(patched_data, parking_json):
    parking_id = 200
    parking = service.decrease_places_count(parking_id)
    assert parking["count_available_places"] == parking_json["count_available_places"]


def test_decrease_places_count_wrong_id(patched_data):
    with pytest.raises(Missing):
        service.decrease_places_count(parking_id=789)
